/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
or see http://www.gnu.org/licenses/agpl.txt.
*/

// OSRM initialization
// [initialization, image prefetching]


// onload initialization routine
OSRM.init = function (options) {
    if( OSRM.checkOldBrowser() == true )
        return;

    options = options || {};
    OSRM.G.guiInputSource = options.inputSource || {};
    OSRM.G.guiDeleteSource = options.deleteSource || {};
    OSRM.G.guiSearchSource = options.searchSource || {};
    OSRM.G.guiInputTarget = options.inputTarget || {};
    OSRM.G.guiDeleteTarget = options.deleteTarget || {};
    OSRM.G.guiSearchTarget = options.searchTarget || {};
    OSRM.G.guiPrinter = options.printer || {};
    OSRM.G.guiHighlightNonames = true;
    OSRM.G.guiMap = options.map || 'map';

	OSRM.showHTML();
	
	OSRM.prefetchImages();
	OSRM.prefetchIcons();
	OSRM.prefetchCSSIcons();
    
	OSRM.GUI.init();
	OSRM.Map.init();
	OSRM.Printing.init();
	OSRM.Routing.init();
	OSRM.RoutingAlternatives.init();
	OSRM.Localization.init();	
 	// check if the URL contains some GET parameter, e.g. for showing a route
 	OSRM.parseParameters();

    OSRM.Map.initPosition();

 	// finalize initialization of map
 	OSRM.Map.initFinally();
 	OSRM.G.EventHandler.fire('onLoad', {});
};


// prefetch images
OSRM.GLOBALS.images = {};
OSRM.prefetchImages = function() {
	var image_list = [	{id:'marker-shadow',					url:'/Scripts/OSRM/leaflet/images/marker-shadow.png'},
	                  	{id:'marker-source',					url:'/Scripts/OSRM/images/marker-source.png'},
						{id:'marker-target',					url:'/Scripts/OSRM/images/marker-target.png'},
						{id:'marker-via',						url:'/Scripts/OSRM/images/marker-via.png'},
						{id:'marker-highlight',					url:'/Scripts/OSRM/images/marker-highlight.png'},
						{id:'marker-source-drag',				url:'/Scripts/OSRM/images/marker-source-drag.png'},
		              	{id:'marker-target-drag',				url:'/Scripts/OSRM/images/marker-target-drag.png'},
		              	{id:'marker-via-drag',					url:'/Scripts/OSRM/images/marker-via-drag.png'},
		              	{id:'marker-highlight-drag',			url:'/Scripts/OSRM/images/marker-highlight-drag.png'},
		              	{id:'marker-drag',						url:'/Scripts/OSRM/images/marker-drag.png'},
		              	{id:'cancel',							url:'/Scripts/OSRM/images/cancel.png'},
		              	{id:'cancel_active',					url:'/Scripts/OSRM/images/cancel_active.png'},
		              	{id:'cancel_hover',						url:'/Scripts/OSRM/images/cancel_hover.png'},
		              	{id:'restore',							url:'/Scripts/OSRM/images/restore.png'},
		              	{id:'restore_active',					url:'/Scripts/OSRM/images/restore_active.png'},
		              	{id:'restore_hover',					url:'/Scripts/OSRM/images/restore_hover.png'},
		              	{id:'up',								url:'/Scripts/OSRM/images/up.png'},
		              	{id:'up_active',						url:'/Scripts/OSRM/images/up_active.png'},
		              	{id:'up_hover',							url:'/Scripts/OSRM/images/up_hover.png'},		
		              	{id:'down',								url:'/Scripts/OSRM/images/down.png'},
		              	{id:'down_active',						url:'/Scripts/OSRM/images/down_active.png'},
		              	{id:'down_hover',						url:'/Scripts/OSRM/images/down_hover.png'},
		              	{id:'config',							url:'/Scripts/OSRM/images/config.png'},
		              	{id:'config_active',					url:'/Scripts/OSRM/images/config_active.png'},
		              	{id:'config_hover',						url:'/Scripts/OSRM/images/config_hover.png'},		              	
		              	{id:'mapping',							url:'/Scripts/OSRM/images/mapping.png'},
		              	{id:'mapping_active',					url:'/Scripts/OSRM/images/mapping_active.png'},
		              	{id:'mapping_hover',					url:'/Scripts/OSRM/images/mapping_hover.png'},		              	
		              	{id:'printer',							url:'/Scripts/OSRM/images/printer.png'},
		              	{id:'printer_active',					url:'/Scripts/OSRM/images/printer_active.png'},
		              	{id:'printer_hover',					url:'/Scripts/OSRM/images/printer_hover.png'},
		              	{id:'printer_inactive',					url:'/Scripts/OSRM/images/printer_inactive.png'},
		              	{id:'zoom_in',							url:'/Scripts/OSRM/images/zoom_in.png'},
		              	{id:'zoom_in_active',					url:'/Scripts/OSRM/images/zoom_in_active.png'},
		              	{id:'zoom_in_hover',					url:'/Scripts/OSRM/images/zoom_in_hover.png'},
		              	{id:'zoom_out',							url:'/Scripts/OSRM/images/zoom_out.png'},
		              	{id:'zoom_out_active',					url:'/Scripts/OSRM/images/zoom_out_active.png'},
		              	{id:'zoom_out_hover',					url:'/Scripts/OSRM/images/zoom_out_hover.png'},		              	
		              	{id:'locations_user',					url:'/Scripts/OSRM/images/locations_user.png'},
		              	{id:'locations_user_active',			url:'/Scripts/OSRM/images/locations_user_active.png'},
		              	{id:'locations_user_hover',				url:'/Scripts/OSRM/images/locations_user_hover.png'},
		              	{id:'locations_user_inactive',			url:'/Scripts/OSRM/images/locations_user_inactive.png'},
		              	{id:'locations_route',					url:'/Scripts/OSRM/images/locations_route.png'},
		              	{id:'locations_route_active',			url:'/Scripts/OSRM/images/locations_route_active.png'},
		              	{id:'locations_route_hover',			url:'/Scripts/OSRM/images/locations_route_hover.png'},
		              	{id:'locations_route_inactive',			url:'/Scripts/OSRM/images/locations_route_inactive.png'},
		              	{id:'layers',							url:'/Scripts/OSRM/images/layers.png'},
		        		{id:'direction_0',						url:'/Scripts/OSRM/images/default.png'},		              	
		              	{id:'direction_1',						url:'/Scripts/OSRM/images/continue.png'},
		              	{id:'direction_2',						url:'/Scripts/OSRM/images/slight-right.png'},
		              	{id:'direction_3',						url:'/Scripts/OSRM/images/turn-right.png'},
		              	{id:'direction_4',						url:'/Scripts/OSRM/images/sharp-right.png'},
		              	{id:'direction_5',						url:'/Scripts/OSRM/images/u-turn.png'},
		              	{id:'direction_6',						url:'/Scripts/OSRM/images/sharp-left.png'},
		              	{id:'direction_7',						url:'/Scripts/OSRM/images/turn-left.png'},
		              	{id:'direction_8',						url:'/Scripts/OSRM/images/slight-left.png'},
		              	{id:'direction_10',						url:'/Scripts/OSRM/images/head.png'},
		        		{id:'direction_11',						url:'/Scripts/OSRM/images/round-about.png'},
		        		{id:'direction_15',						url:'/Scripts/OSRM/images/target.png'},
		        		{id:'osrm-logo',						url:'/Scripts/OSRM/images/osrm-logo.png'},
		        		{id:'selector',							url:'/Scripts/OSRM/images/selector.png'}
	               ];
		
	for(var i=0; i<image_list.length; i++) {
		OSRM.G.images[image_list[i].id] = new Image();
		OSRM.G.images[image_list[i].id].src = image_list[i].url;
	}
};


// prefetch icons
OSRM.GLOBALS.icons = {};
OSRM.prefetchIcons = function() {
	var icon_list = [	{id:'marker-source',					image_id:'marker-source'},
						{id:'marker-target',					image_id:'marker-target'},
						{id:'marker-via',						image_id:'marker-via'},
						{id:'marker-highlight',					image_id:'marker-highlight'},
						{id:'marker-source-drag',				image_id:'marker-source-drag'},
						{id:'marker-target-drag',				image_id:'marker-target-drag'},
						{id:'marker-via-drag',					image_id:'marker-via-drag'}
						//{id:'marker-drag',						image_id:'marker-drag'}				// special treatment because of size and shadow
	              ];
	
	var LabelMarkerIcon = L.LabelMarkerIcon.extend({
		options: {
			shadowUrl: OSRM.G.images["marker-shadow"].getAttribute("src"),
			iconSize:     [25, 41],
			shadowSize:   [41, 41],
			iconAnchor:   [13, 41],
			shadowAnchor: [13, 41],
			popupAnchor:  [0, -33]
		} });
	for(var i=0; i<icon_list.length; i++) {
		OSRM.G.icons[icon_list[i].id] = new LabelMarkerIcon({iconUrl: OSRM.G.images[icon_list[i].image_id].getAttribute("src")});
	}
	
	// special values for drag marker
	OSRM.G.icons['marker-drag'] = new L.LabelMarkerIcon({ iconUrl: OSRM.G.images["marker-drag"].getAttribute("src"), iconSize: new L.Point(18, 18) });
	OSRM.G.icons['marker-me'] = new L.LabelMarkerIcon({ iconUrl: OSRM.G.images["marker-drag"].getAttribute("src"), iconSize: new L.Point(25, 25) });
};


// set css styles for images
OSRM.prefetchCSSIcons = function() {
	var css_list = [
	                	{ id:'#gui-printer-inactive',			image_id:'printer_inactive'},
	                	{ id:'#gui-printer',					image_id:'printer'},
	                	{ id:'#gui-printer:hover',				image_id:'printer_hover'},
	                	{ id:'#gui-printer:active',				image_id:'printer_active'},
                	
	                	{ id:'.gui-zoom-in',					image_id:'zoom_in'},
	                	{ id:'.gui-zoom-in:hover',				image_id:'zoom_in_hover'},
	                	{ id:'.gui-zoom-in:active',				image_id:'zoom_in_active'},
	                	
	                	{ id:'.gui-zoom-out',					image_id:'zoom_out'},
	                	{ id:'.gui-zoom-out:hover',				image_id:'zoom_out_hover'},
	                	{ id:'.gui-zoom-out:active',			image_id:'zoom_out_active'},
	                	
	                	{ id:'.gui-locations-user-inactive',	image_id:'locations_user_inactive'},
	                	{ id:'.gui-locations-user',				image_id:'locations_user'},
	                	{ id:'.gui-locations-user:hover',		image_id:'locations_user_hover'},
	                	{ id:'.gui-locations-user:active',		image_id:'locations_user_active'},
	                	
	                	{ id:'.gui-locations-route-inactive',	image_id:'locations_route_inactive'},
	                	{ id:'.gui-locations-route',			image_id:'locations_route'},
	                	{ id:'.gui-locations-route:hover',		image_id:'locations_route_hover'},
	                	{ id:'.gui-locations-route:active',		image_id:'locations_route_active'},
	                	
	                	{ id:'.gui-layers',						image_id:'layers'},
	                	
	                	{ id:'.cancel-marker',					image_id:'cancel'},
	                	{ id:'.cancel-marker:hover',			image_id:'cancel_hover'},
	                	{ id:'.cancel-marker:active',			image_id:'cancel_active'},
	                	
	                	{ id:'.up-marker',						image_id:'up'},
	                	{ id:'.up-marker:hover',				image_id:'up_hover'},
	                	{ id:'.up-marker:active',				image_id:'up_active'},
	                	
	                	{ id:'.down-marker',					image_id:'down'},
	                	{ id:'.down-marker:hover',				image_id:'down_hover'},
	                	{ id:'.down-marker:active',				image_id:'down_active'},
	                	
	                	{ id:'#input-mask-header',				image_id:'osrm-logo'},
	                	{ id:'.styled-select',					image_id:'selector'},
	                	
	                	{ id:'#config-handle-icon',				image_id:'config'},
	                	{ id:'#config-handle-icon:hover',		image_id:'config_hover'},
	                	{ id:'#config-handle-icon:active',		image_id:'config_active'},
	                	           	
	                	{ id:'#mapping-handle-icon',			image_id:'mapping'},
	                	{ id:'#mapping-handle-icon:hover',		image_id:'mapping_hover'},
	                	{ id:'#mapping-handle-icon:active',		image_id:'mapping_active'},
	                	          	
	                	{ id:'#main-handle-icon',				image_id:'restore'},
	                	{ id:'#main-handle-icon:hover',			image_id:'restore_hover'},
	                	{ id:'#main-handle-icon:active',		image_id:'restore_active'}	                	
	                ];
	
	var stylesheet = OSRM.CSS.getStylesheet("main.css");
	for(var i=0; i<css_list.length; i++) {
		OSRM.CSS.insert( stylesheet, css_list[i].id, "background-image:url("+ OSRM.G.images[css_list[i].image_id].getAttribute("src") + ");" );
	}
};


// check if an old browser was used and show the error message
OSRM.checkOldBrowser = function() {
	if( OSRM.Browser.IE6_7 == -1 )
		return false;
	
	document.getElementById("old-browser-warning").style.display = "block";
	return true;
};


// only show html content of website, if javascript is active 
OSRM.showHTML = function() {
	document.getElementById("map").style.display = "block";
	//document.getElementById("gui").style.display = "block";
};


// parse URL GET parameters
OSRM.parseParameters = function() {
    var called_url = document.location.search.substr(1, document.location.search.length);

    // state, if GET parameters specify a different initial position
    OSRM.G.initial_position_override = false;

    // reject messages that are clearly too long or too small
    if (called_url.length > 1000 || called_url.length == 0)
        return;

    // storage for parameter values
    var params = {};

    // default values for parameters for which using PARAMS_VALUE || DEFAULT_VALUE is not an option (as they can evaluate to 0)
    params.active_routing_engine = OSRM.DEFAULTS.ROUTING_ENGINE;

    // parse input
    var splitted_url = called_url.split('&');
    for (var i = 0; i < splitted_url.length; i++) {
        var name_val = splitted_url[i].split('=');
        if (name_val.length != 2)
            continue;

        if (name_val[0] == 'hl') {
            OSRM.Localization.setLanguage(name_val[1]);
        } else if (name_val[0] == 'df') {
            var type = parseInt(name_val[1]);
            if (type != 0 && type != 1)
                return;
            OSRM.GUI.setDistanceFormat(type);
        } else if (name_val[0] == 'loc') {
            var coordinates = unescape(name_val[1]).split(',');
            if (coordinates.length != 2 || !OSRM.Utils.isLatitude(coordinates[0]) || !OSRM.Utils.isLongitude(coordinates[1]))
                return;
            params.positions = params.positions || [];
            params.positions.push(new L.LatLng(coordinates[0], coordinates[1]));
        } else if (name_val[0] == 'dest') {
            var coordinates = unescape(name_val[1]).split(',');
            if (coordinates.length != 2 || !OSRM.Utils.isLatitude(coordinates[0]) || !OSRM.Utils.isLongitude(coordinates[1]))
                return;
            params.destination = new L.LatLng(coordinates[0], coordinates[1]);
        } else if (name_val[0] == 'destname') {
            params.destination_name = decodeURI(name_val[1]).replace(/<\/?[^>]+(>|$)/g, ""); // discard tags	
        } else if (name_val[0] == 'z') {
            var zoom_level = Number(name_val[1]);
            if (zoom_level < 0 || zoom_level > 18)
                return;
            params.zoom = zoom_level;
        } else if (name_val[0] == 'center') {
            var coordinates = unescape(name_val[1]).split(',');
            if (coordinates.length != 2 || !OSRM.Utils.isLatitude(coordinates[0]) || !OSRM.Utils.isLongitude(coordinates[1]))
                return;
            params.center = new L.LatLng(coordinates[0], coordinates[1]);
        } else if (name_val[0] == 'alt') {
            var active_alternative = Number(name_val[1]);
            if (active_alternative < 0 || active_alternative > OSRM.RoutingAlternatives > 10)	// using 10 as arbitrary upper limit
                return;
            params.active_alternative = active_alternative;
        } else if (name_val[0] == 're') {
            var active_routing_engine = Number(name_val[1]);
            if (active_routing_engine < 0 || active_routing_engine >= OSRM.DEFAULTS.ROUTING_ENGINES.length)
                return;
            params.active_routing_engine = active_routing_engine;
        }
    }

    // stop if in maintenance mode
    if (OSRM.GUI.inMaintenance() == true)
        return;

    // case 1: destination given
    if (params.destination) {
        var index = OSRM.G.markers.setTarget(params.destination);
        if (params.destination_name)
            OSRM.G.markers.route[index].description = params.destination_name;	// name in GUI will be set when languages are loaded
        else
            OSRM.Geocoder.updateAddress(OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG);
        OSRM.G.markers.route[index].show();
        OSRM.G.markers.route[index].centerView(params.zoom);
        OSRM.G.initial_position_override = true;
        return;
    }

    // case 2: locations given
    if (params.positions) {
        // draw via points
        if (params.positions.length > 0) {
            OSRM.G.markers.setSource(params.positions[0]);
            OSRM.Geocoder.updateAddress(OSRM.C.SOURCE_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG);
        }
        if (params.positions.length > 1) {
            OSRM.G.markers.setTarget(params.positions[params.positions.length - 1]);
            OSRM.Geocoder.updateAddress(OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG);
        }
        for (var i = 1; i < params.positions.length - 1; i++)
            OSRM.G.markers.setVia(i - 1, params.positions[i]);
        for (var i = 0; i < OSRM.G.markers.route.length; i++)
            OSRM.G.markers.route[i].show();

        // center on route (support for old links) / move to given view (new behaviour)
        if (params.zoom == null || params.center == null) {
            var bounds = new L.LatLngBounds(params.positions);
            OSRM.G.map.fitBoundsUI(bounds);
        } else {
            OSRM.G.map.setView(params.center, params.zoom);
        }

        // set active alternative (if via points are set or alternative does not exists, fallback to shortest route)
        OSRM.G.active_alternative = params.active_alternative || 0;

        // set routing server
        OSRM.GUI.setRoutingEngine(params.active_routing_engine);

        // compute route
        OSRM.Routing.getRoute({ keepAlternative: true });
        OSRM.G.initial_position_override = true;
        return;
    }

    // default case: do nothing	
    return;
};
// onload event
//OSRM.Browser.onLoadHandler( OSRM.init );
